package com.ruoyi.wechart.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.device.domain.DeviceBorrow;
import com.ruoyi.device.domain.DeviceInfo;
import com.ruoyi.device.domain.DeviceType;
import com.ruoyi.device.service.IDeviceBorrowService;
import com.ruoyi.device.service.IDeviceInfoService;
import com.ruoyi.device.service.IDeviceTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 微信小程序访问设备借用controller
 * @author
 * @date
 */
@RestController
@RequestMapping("/wechart/device")
public class WxDeviceController extends BaseController {
    @Autowired
    private IDeviceTypeService typeService;

    @Autowired
    private IDeviceInfoService deviceService;

    @Autowired
    private IDeviceBorrowService borrowService;

    /**
     * 设备分类别列表
     * @return
     */
    @GetMapping("/typeList")
    public AjaxResult typeList() {
        return success(typeService.getTypeList(new DeviceType()));
    }

    /**
     * 设备列表
     * @param typeId 设备类别id
     * @param deviceName 设备名称
     * @param deviceStatus 设备状态
     * @return
     */
    @GetMapping("/deviceList")
    public AjaxResult deviceList(Long typeId, String deviceName, Integer deviceStatus) {
        DeviceInfo deviceInfo = new DeviceInfo();
        if (typeId != null) {
            deviceInfo.setTypeId(typeId);
        }
        if (StringUtils.isNotBlank(deviceName)) {
            deviceInfo.setDeviceName(deviceName);
        }
        if (deviceStatus != null) {
            deviceInfo.setDeviceStatus(deviceStatus);
        }
        return success(deviceService.getDeviceList(deviceInfo));
    }

    /**
     * 获取设备详情
     * @param deviceId 设备id
     * @return
     */
    @GetMapping("/deviceInfo")
    public AjaxResult deviceInfo(Long deviceId) {
        DeviceInfo device = deviceService.getDeviceById(deviceId);
        return success(device);
    }

    /**
     * 借用记录表
     * @param returnStatus
     * @return
     */
    @GetMapping("/borrowList")
    public AjaxResult borrowList(Integer returnStatus) {
        Long userId = SecurityUtils.getUserId();
        DeviceBorrow borrow = new DeviceBorrow();
        borrow.setUserId(userId);
        if (returnStatus != null) {
            borrow.setReturnStatus(returnStatus);
        }
        return success(borrowService.getBorrowList(borrow));
    }

    /**
     * 设备借用
     * @param borrow
     * @return
     */
    @PostMapping("/borrowDevice")
    public AjaxResult deviceBorrow(@RequestBody DeviceBorrow borrow) {
        Long userId = SecurityUtils.getUserId();
        borrow.setUserId(userId);
        borrow.setBorrowTime(DateUtils.getNowDate());
        borrow.setReturnStatus(0);
        return toAjax(borrowService.addBorrow(borrow));
    }

    /**
     * 归还设备
     * @param borrow
     * @return
     */
    @PostMapping("/returnDevice")
    public AjaxResult deviceReturn(@RequestBody DeviceBorrow borrow) {
        Long userId = SecurityUtils.getUserId();
        borrow.setUserId(userId);
        borrow.setReturnStatus(2);
        return toAjax(borrowService.editBorrow(borrow));
    }
}
